from api_test.api.feishu_api import FeiShuApi
import allure

class CalendarApi(FeiShuApi):
    def __init__(self):
        super().__init__()
        self.calendar_ids = []

    # 创建日历
    def create(self, summary="测试日历标题", description="测试日历描述", permissions="private", color=-1, summary_alias=None):
        path = "/open-apis/calendar/v4/calendars"
        data = {
            "summary": summary,
            "description": description,
            "permissions": permissions,
            "color": color,
            "summary_alias": summary_alias
        }
        # 发创建请求并将返回结果转换为json格式
        res = self.send_request("POST", self.base_url + path, json=data)
        # print(res.text)
        # 判断是否创建成功， 成功则记录calendar_id
        try:
            self.json_data = res.json()
            if res.status_code == 200:
                if self.jsonpath(expr="$.code")[0] == 0:
                    calendar_id = self.jsonpath(expr="$..calendar_id")[0]
                    self.calendar_ids.append(calendar_id)
                    print(self.calendar_ids)
                    return self.json_data
            else:
                # print(f"日历创建失败：{res.status_code} {res.text}")
                return self.json_data
        except Exception as E:
            print(res.status_code, res.text)

    # 根据 calendar_id 删除日历
    def delete(self, calendar_id):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}"
        res = self.send_request("DELETE", self.base_url + path)
        # print(res)
        return res.json()

    def deletes(self, *args):
        # print(args)
        if args[0] == "all":
            # 返回的结果保存在self.json_data
            self.list()
            # self.jsonpath()默认读取self.json_data
            print(self.json_data)
            self.calendar_ids = self.jsonpath("$..calendar_id")
            # print(self.calendar_ids)
            for calendar_id in self.calendar_ids:
                self.delete(calendar_id)
        for calendar_id in args:
            self.delete(calendar_id)
        self.list()
        if len(self.jsonpath("$..calendar_id")) > 1:
            return False
        return True

    # 获取日历
    def get(self, calendar_id):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}"
        res = self.send_request("GET", self.base_url + path)
        return res.json()

    # 获取日历列表
    def list(self, page_size=100, page_token='', sync_token=''):
    # def list(self):
        path = "/open-apis/calendar/v4/calendars"
        data = {
            "page_size": page_size,
            "page_token": page_token,
            "sync_token": sync_token
        }
        self.json_data = self.send_request("GET", self.base_url + path, json=data).json()
        # print(res.json())
        return self.json_data

    # 根据 calendar_id 更新日历
    def update(self, calendar_id, summary, description, permissions, color, summary_alias):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}"
        data = {
            "summary": summary,
            "description": description,
            "permissions": permissions,
            "color": color,
            "summary_alias": summary_alias
        }
        res = self.send_request("PATCH", self.base_url + path, json=data)
        return res.json()

    # 搜索日历
    def search(self, query):
        path = "/open-apis/calendar/v4/calendars/search"
        data = {
            "query": query
        }
        res = self.send_request("POST", self.base_url + path, json=data)
        return res.json()

    # 订阅日历
    def subscribe(self, calendar_id):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}/unsubscribe"
        res = self.send_request("POST", self.base_url + path)
        return res.json()

    # 取消订阅日历
    def unsubscribe(self, calendar_id):
        path = f"/open-apis/calendar/v4/calendars/{calendar_id}/subscribe"
        res = self.send_request("POST", self.base_url + path)
        return res.json()

    # 订阅日历变更事件
    def subscription(self):
        path = "/open-apis/calendar/v4/calendars/subscription"
        res = self.send_request("POST", self.base_url + path)
        return res.json()



